package chuangyi.com.org.ViewModel;

import android.content.Context;
import android.databinding.Bindable;

import com.solidfire.gson.internal.LinkedTreeMap;

import java.util.ArrayList;

import chuangyi.com.org.DOMIHome.BR;
import chuangyi.com.org.DOMIHome.LoadingDialog;
import chuangyi.com.org.DOMIHome.R;
import chuangyi.com.org.DOMIHome.Utils.Utils;
import chuangyi.com.org.DOMIHome.Utils.WebApi;
import chuangyi.com.org.DOMIHome.adapter.XListAdapterInterface;
import chuangyi.com.org.DOMIHome.adapter.XListBindingAdapter;

/**
 * Created by zxh on 16/11/25.
 */

public class MyRewardModel extends XListModel {
    private boolean question;
    private boolean reward;
    private boolean all;
    private boolean my;
    private boolean other;
    private boolean loading;

    @Bindable
    public boolean isQuestion() {
        return question;
    }

    public void setQuestion(boolean question) {
        if (this.question != question) {
            this.question = question;
            if (this.question) {
                setReward(false);
                getAdapter().clear(true);
                setCurrentPage(1);
                load(1);
            }
        }
        notifyPropertyChanged(BR.question);
    }

    @Bindable
    public boolean isReward() {
        return reward;
    }

    public void setReward(boolean reward) {
        if (this.reward != reward) {
            this.reward = reward;
            if (this.reward) {
                setQuestion(false);
                getAdapter().clear(true);
                setCurrentPage(1);
                load(1);
            }
        }
        notifyPropertyChanged(BR.reward);
    }

    @Bindable
    public boolean isAll() {
        return all;
    }

    public void setAll(boolean all) {
        if (all != this.all) {
            this.all = all;
            if (all) {
                setMy(false);
                setOther(false);
                getAdapter().clear(true);
                setCurrentPage(1);
                load(1);
            }
        }
        notifyPropertyChanged(BR.all);
    }

    @Bindable
    public boolean isMy() {
        return my;
    }

    public void setMy(boolean my) {
        if (my != this.my) {
            this.my = my;
            if (this.my) {
                setOther(false);
                setAll(false);
                getAdapter().clear(true);
                setCurrentPage(1);
                load(1);
            }
        }
        notifyPropertyChanged(BR.my);
    }

    @Bindable
    public boolean isOther() {
        return other;
    }

    public void setOther(boolean other) {
        if (other != this.other) {
            this.other = other;
            if (this.other) {
                setMy(false);
                setAll(false);
                getAdapter().clear(true);
                setCurrentPage(1);
                load(1);
            }
        }
        notifyPropertyChanged(BR.other);
    }

    public MyRewardModel(Context context) {
        super(context);
        super.getHandler().setTitle("我的悬赏/提问");
        reward = true;
        all = true;
    }

    @Override
    public void load(final int page) {
        if (loading) {
            return;
        }
        setNoMore();
        loading = true;
        final boolean isQuestion = this.question;
        final Context context = getContext();
        LoadingDialog.getInstance(context).show();
        final String type = all ? "all" : (my ? "my" : "other");
        new Thread(new Runnable() {
            @Override
            public void run() {
                XListAdapterInterface adapter = getAdapter();
                adapter.setVariableId(BR.commonItem);
                ArrayList list = null;
                if (isQuestion) {
                    list = loadQuestion(type);
                } else {
                    list = loadReward(type);
                }
                try {
                    for (Object message : list) {
                        LinkedTreeMap map = (LinkedTreeMap) message;
                        CommonListItem item = new CommonListItem(context, R.layout.i_common, false);
                        item.setTitle((String) map.get("title"));
                        item.setId(Utils.getIntValue(map.get("questionid")));
                        item.setBrowser(Utils.getIntValue(map.get("answercoun")));
                        item.setSystime(Utils.getDateFromTimestamp(map.get("systime")));
                        item.setType(CommonListItem.TYPE_MY_ASK);
                        adapter.add(item);
                    }
                } catch (Exception ex) {
                    showToast("解析数据时发生错误");
                }
                if (list.size() > 0) {
                    setCurrentPage(page);
                }
                notifyDataSetChanged(list);
                LoadingDialog.getInstance(context).dismiss();
                loading = false;
            }
        }).start();
    }

    private ArrayList loadQuestion(String type) {
//        questionid：问题id，title:标题，startdate:提问日期,answercoun：回复数
        return WebApi.getMyQuestion(getContext(), type);
    }

    private ArrayList loadReward(String type) {
        return WebApi.getMyReward(getContext(), type);
    }
}
